package object;

import java.sql.*;
import java.text.MessageFormat;
import java.util.Scanner;

public class Test1 {
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?useSSL" +
                    "=false&serverTimezone=UTC", "root", "237912");
            statement = connection.createStatement();
            System.out.println("请输入要查询的学生名字：");
            Scanner scanner = new Scanner(System.in);
            String inputName = scanner.next();
            String sql = "select id, name, age, gender from student where name = '" + inputName + "'";
            resultSet = statement.executeQuery(sql);
            if (resultSet.next()) {
                long id = resultSet.getLong(1);
                String name = resultSet.getString(2);
                int age = resultSet.getInt(3);
                boolean gender = resultSet.getBoolean(4);
                System.out.println(MessageFormat.format("id: {0}, name: {1}, age: {2}, gender: {3}"
                        , id, name, age, gender));
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

